package com.darren.framework.controller;


import com.darren.framework.Exception.ArchiveException;
import com.darren.framework.Exception.TokenException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "Filter异常处理控制器", tags = "Filter异常处理控制器", description = "Filter异常处理控制器")
@RestController
public class FilterExceptionController extends BaseController {

    /**
     * 过虑器中的异常，全局异常处理器，捕获不到，
     * 所以将异常放到 request 里，再跳转到这里，将异常抛出
     * 这样全局异常处理器就能捕获到，统一处理了
     * 未例，见 MyStatelessShiroFilter
     *
     * @return
     */
    @ApiOperation(value = "", notes = "Filter异常处理控制器")
    @RequestMapping(value = "/filter/exception", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.HEAD, RequestMethod.PUT, RequestMethod.PATCH, RequestMethod.TRACE})
    public void filterExceptionHandler(HttpServletRequest request, HttpServletResponse response) {
        Object exception = request.getAttribute("exception");
        if (exception != null && exception instanceof TokenException) {
            throw (TokenException) exception;
        } else {
            throw new ArchiveException("未知错误");
        }
    }

}
